import 'dart:convert';
import 'package:flutter/material.dart';
import '../routes.dart';
import '../utils/common_toast.dart';
// import '../utils/dio_http.dart';
import '../utils/funtions.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({Key key}) : super(key: key);

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  bool showPassword = false;
  var usernameController = new TextEditingController();
  var passwordController = new TextEditingController();
  var repeatController = new TextEditingController();

  _registerHandle() async {
    var username = usernameController.text;
    var password = passwordController.text;
    var repeat = repeatController.text;

    if (password != repeat) {
      CommonToast.showToast('两次密码不一样！');
      return;
    }

    if (stringIsNullOrEmpty(username) || stringIsNullOrEmpty(password)) {
      CommonToast.showToast('用户名和密码不能为空！');
      return;
    }

    const url = '/user/registered';
    var params = {"username": username, "password": password};
    /* var res = await DioHttp.of(context).post(url, params);
    var resString = json.decode(res.toString());

    int status = resString['status'];
    String msg = resString['description'] ?? "内容为空";
    CommonToast.showToast(msg);
    if (status.toString().startsWith('2')) {
      Navigator.of(context).pushReplacementNamed('login');
    } */
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('注册'),
      ),
      body: SafeArea(
        minimum: EdgeInsets.all(30),
        child: ListView(
          children: [
            TextField(
              controller: usernameController,
              decoration: InputDecoration(labelText: '用户名', hintText: '请输入用户名'),
            ),
            Padding(padding: EdgeInsets.all(10)),
            TextField(
              controller: passwordController,
              obscureText: !showPassword,
              decoration: InputDecoration(labelText: '密码', hintText: '请输入密码'),
            ),
            Padding(padding: EdgeInsets.all(10)),
            TextField(
              controller: repeatController,
              obscureText: !showPassword,
              decoration: InputDecoration(
                labelText: '确认密码',
                hintText: '请输入密码',
              ),
            ),
            Padding(padding: EdgeInsets.all(10)),
            // ignore: deprecated_member_use
            RaisedButton(
              color: Colors.green,
              onPressed: () {
                _registerHandle();
              },
              child: Text(
                '注册',
                style: TextStyle(color: Colors.white),
              ),
            ),
            Padding(padding: EdgeInsets.all(10)),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('有账号，'),
                // ignore: deprecated_member_use
                FlatButton(
                    onPressed: () {
                      Navigator.pushReplacementNamed(context, Routes.login);
                    },
                    child: Text(
                      '去登录',
                      style: TextStyle(color: Colors.green),
                    ))
              ],
            )
          ],
        ),
      ),
    );
  }
}
